// 
//  ViewNoteCommand.cs
//  
//  Author:
//       Ray Shaw <demogodyou@gmail.com>
// 
//  Copyright (c) 2009 Ray Shaw(xiaolei)
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3053
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using SharpNote.Core.Command;
using SharpNote.Core.Data;
using SharpNote.Core.Entity;
using SharpNote.Core.Config;

namespace SharpNote.Core
{
	/// <summary>
	/// Display saved note.
	/// </summary>
	[CommandAttribute("ViewNote", "vnote")]
	public class ViewNoteCommand: CommandBase
	{

		public ViewNoteCommand ()
		{
		}
		
		public override void Execute(ref object result, params object[] parameters)
		{
			result = true;
			bool isEmpty = true;
			NoteInfo noteInfo = null; 

			//display the latest note.
			if(parameters.Length == 0)
			{
				noteInfo = DbObjectMappingHelper.Query<NoteInfo>("SELECT * FROM NoteInfo order by CreateTime DESC limit 1",
				                                                 out isEmpty);
				if(!isEmpty)
				{
					noteInfo.Decrypt(SysConfig.GetInstance().PrivateKeyPlainText);
					Console.WriteLine("Latest note:");
					Console.WriteLine("\n" + noteInfo.ToString());
				}else
				{
					Console.WriteLine(">>No note.");
				}
				
				return;
			}
			if(parameters.Length == 1)
			{
			    string param1 = parameters[0].ToString();
				
				if(param1.Equals("/?", StringComparison.CurrentCultureIgnoreCase)
				   || param1.Equals("/help", StringComparison.CurrentCultureIgnoreCase))
				{
					Console.WriteLine(GetHelpeText());
					return;
				}
				
				isEmpty = true;
			    noteInfo = DbObjectMappingHelper.Query<NoteInfo>("select * from NoteInfo where Id = @id",
				                                                 out isEmpty,
				                                                 DataAccessService.CreateDbParameter("@id", System.Data.DbType.String, param1));
				if(!isEmpty)
				{
					noteInfo.Decrypt(SysConfig.GetInstance().PrivateKeyPlainText);
					Console.WriteLine("\n" + noteInfo.ToString());
				}else
				{
					Console.WriteLine(">>No matched note.");
				}
			}else if(parameters.Length == 2)
			{
			    Console.WriteLine("Invalid arguments.");
				Console.WriteLine(GetHelpeText());
				return;
			}
		
		}
		
		public string GetHelpeText()
		{
			return "\nCommand: \n  ViewNote [Id]\nSummary: \n  Display saved note.\nAlias: \n  vnote\nParameters: \n  Id: note id.\n";
		}
		
		public string GetAboutText()
		{
			return "\nCreated by Ray Shaw(demogodyou@gmail.com.";
		}
	}
}
